package com.Khan.SocialRescueSys.config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Khan Liu
 * @date 2022/4/5 22:09
 */
@Configuration
public class ShiroConfig {

    /**ShiroFilterFactoryBean*/
    @Bean(name = "shiroFilterFactoryBean")
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("SecurityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(defaultWebSecurityManager);
        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/admin/login","anon");
        filterMap.put("/admin/**","authc");
        bean.setFilterChainDefinitionMap(filterMap);
        bean.setLoginUrl("/admin");
        bean.setSuccessUrl("/admin/index");
        return bean;
    }

    /**DefaultWebSecurityManager*/
    @Bean(name = "SecurityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager SecurityManager = new DefaultWebSecurityManager();
        /**关联realm*/
        SecurityManager.setRealm(userRealm);
        return SecurityManager;
    }

    /**创建realm对象*/
    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }


}
